import java.util.Arrays;
class Zcsh {
    public int maxEnvelopes(int[][] envelopes) {

        if (envelopes == null || envelopes.length == 0) return 0;
        Arrays.sort(envelopes, (int[] a, int[] b) -> {
            return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0];
        });

        int[] heigh = new int[envelopes.length];
        for (int i = 0; i < heigh.length; i++) {
            heigh[i] = envelopes[i][1];
        }

        int len = 0;
        for(int num : heigh) {
            int i = Arrays.binarySearch(heigh, 0, len, num);
            if(i < 0) {
                i = -(i + 1);
            }
            heigh[i] = num;
            if(i == len) {
                len++;
            }
        }
        return len;

    }
}